<template>
	<el-row type="flex" class="row-bg" justify="center">
		<el-col :xl="6" :lg="7">
			<h2>欢迎来到VueAdmin管理系统</h2>
			<el-image :src="require('@/assets/logo.png')" style="height: 180px; width: 180px;"></el-image>
		</el-col>

		<el-col :span="1">
			<el-divider direction="vertical"></el-divider>
		</el-col>
		<el-col :xl="6" :lg="7">
			<el-form :model="loginForm" :rules="rules" ref="logForm" label-width="80px">
				<el-form-item label="用户名" prop="username" style="width: 380px;">
					<el-input v-model="loginForm.username"></el-input>
				</el-form-item>
				<el-form-item label="密码" prop="password"  style="width: 380px;">
					<el-input v-model="loginForm.password" type="password"></el-input>
				</el-form-item>
				<el-form-item label="验证码" prop="code"  style="width: 380px;">
					<el-input v-model="loginForm.code"  style="width: 172px; float: left" maxlength="5"></el-input>
					<el-image :src="captchaImg" class="captchaImg" @click="getCaptcha"></el-image>
				</el-form-item>

				<el-form-item>
					<el-button type="primary" @click="submitForm('logForm')">登录</el-button>
					<el-button @click="resetForm('logForm')">重置</el-button>
				</el-form-item>
			</el-form>
		</el-col>
	</el-row>

</template>

<script>
	import qs from 'qs'

	export default {
		name: "Login",
		data() {
			return {
				loginForm: {
					username: 'admin',
					password: '111111',
					code: '',
					key: ''
				},
				rules: {
					username: [
						{ required: true, message: '请输入用户名', trigger: 'blur' }
					],
					password: [
						{ required: true, message: '请输入密码', trigger: 'blur' }
					],
					code: [
						{ required: true, message: '请输入验证码', trigger: 'blur' },
						{ min: 5, max: 5, message: '长度为 5 个字符', trigger: 'blur' }
					],
				},
				captchaImg: ""
			};
		},
		methods: {
			submitForm(formName) {
				//validate自定义校验
				this.$refs[formName].validate((valid) => {	
					if (valid) {
						this.$axios.post('/login?' + qs.stringify(this.loginForm)).then(res => {
							// console.log(res);
							
							// const jwt = res.headers['authorization'];
							const jwt = res.data.data.token_type + " " + res.data.data.access_token
							// const jwt = "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJodHRwOi8vc2NoZW1hcy54bWxzb2FwLm9yZy93cy8yMDA1LzA1L2lkZW50aXR5L2NsYWltcy9uYW1lIjoiYWRtaW4iLCJodHRwOi8vc2NoZW1hcy5taWNyb3NvZnQuY29tL3dzLzIwMDgvMDYvaWRlbnRpdHkvY2xhaW1zL3JvbGUiOlsiUk9MRV9ub3JtYWwiLCJST0xFX2FkbWluIiwiUk9MRV9tYW5hZ2UiLCJzeXM6bWFuYWdlIiwic3lzOnVzZXI6bGlzdCIsInN5czpyb2xlOmxpc3QiLCJzeXM6bWVudTpsaXN0Iiwic3lzOnRvb2xzIiwic3lzOmRpY3Q6bGlzdCIsInN5czpyb2xlOnNhdmUiLCJzeXM6dXNlcjpzYXZlIiwic3lzOnVzZXI6dXBkYXRlIiwic3lzOnVzZXI6ZGVsZXRlIiwic3lzOnVzZXI6cm9sZSIsInN5czp1c2VyOnJlcGFzcyIsInN5czpyb2xlOnVwZGF0ZSIsInN5czpyb2xlOmRlbGV0ZSIsInN5czpyb2xlOnBlcm0iLCJzeXM6bWVudTpzYXZlIiwic3lzOm1lbnU6dXBkYXRlIiwic3lzOm1lbnU6ZGVsZXRlIiwic3lzOnRlc3Q6bGlzdCIsInN5czp0ZXN0MyJdLCJuYmYiOjE2NzU1MDAwMjUsImV4cCI6MTY3NTUwMDA2MSwiaXNzIjoiSldUQmVhcmVyLkF1dGgiLCJhdWQiOiJhcGkuYXV0aCJ9.gF-P6aNZS8t2ZGJVhmYI8oImz-zSBc3YO9-1r1jibh0"
							// 将jwt存储到应用store中
							this.$store.commit('SET_TOKEN', jwt)
							this.$router.push("/index")
						}).catch(error => {
							 this.getCaptcha();
							 // console.log(error);
						})
					} else {
						this.getCaptcha();
						// console.log('error submit!!');
						return false;
					}
				});
			},
			resetForm(formName) {
				this.$refs[formName].resetFields();
			},
			getCaptcha() {
				this.$axios.get('/captcha').then(res => {
					// console.log("/captcha")
					// console.log(res)

					this.loginForm.key = res.data.data.token;
					this.captchaImg = res.data.data.captchaImg;
					this.loginForm.code = '';
				})
			}
		},
		created() {
			this.getCaptcha()
		}
	}
</script>

<style scoped>

	.el-row {
		background-color: #fafafa;
		height: 100%;
		display: flex;
		align-items: center;
		text-align: center;
		justify-content: center;
	}

	.el-divider {
		height: 200px;
	}

	.captchaImg {
		float: left;
		margin-left: 8px;
		border-radius: 4px;
		width: 120px;
		height: 40px;
	}

</style>
